home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / cgazv4n2.zip / CPP.ZIP / LLTEST.CXX < prev    next >
C/C++ Source or Header  |  1989-08-27  |  1KB  |  51 lines

  1. // LLTEST.CXX : test for generic linked-list
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include "llist.hxx"
  5.  
  6. class word {
  7.   char * w;
  8. public:
  9.   word(char * wrd) {
  10.     w = new char[strlen(wrd) + 1];
  11.     strcpy(w, wrd);
  12.   }
  13.   ~word() { puts(w); delete w; puts("~word() called"); }
  14.   void print() { printf("%s", w); }
  15. };
  16.  
  17. class wordlist : public llist {
  18. protected:
  19.   void delete_data(void * ww) {
  20.     delete (word *)ww;
  21.   }
  22. public:
  23.   // Modify the functions to take and return words:
  24.   void append(word * el) { llist::append(el); }
  25.   void insert(word * el) { llist::insert(el); }
  26.   word * value() { return (word *)llist::value(); }
  27.   // add a new function to print the whole list:
  28.   void print_list() {
  29.     reset();
  30.     while(!end()) {
  31.       value()->print();
  32.       puts("");
  33.       next();
  34.     }
  35.   }
  36. };
  37.  
  38. #define ADD(X) words.append(new word(#X))
  39.  
  40. main() {
  41.   wordlist words;
  42.   ADD(this); ADD(is); ADD(a); /* ADD(test); */ ADD(of); 
  43.   ADD(the); ADD(linked); ADD(list); ADD(class);
  44.   words.print_list();
  45.   words.reset();
  46.   for(int i = 0 ; i < 3; i++)
  47.     words.next();
  48.   words.insert(new word("test"));
  49.   words.print_list();
  50. }
  51.